/************************************************************************/
/*  									*/
/*  writecdbk_MIS.c							*/
/*									*/
/*  Tom Lookabaugh							*/
/*  August 14, 1987							*/
/*  Last Revision: August 15, 1987					*/
/*									*/
/*  DESCRIPTION								*/
/*	Modified Itakura-Saito version.					*/
/*	Writes the codebook (filter autocorrelations), then calculates	*/
/*	reflection coefficients and writes them.			*/
/*  RETURNS								*/
/*	No significant value.  						*/
/*  CALLS								*/
/*	automethx(), error()						*/
/*									*/
/************************************************************************/

#include "stdvq.h"

writecdbk (cdbk, cdbksz, cdbkup) 

    double *cdbk;	/* codebook					*/
    int cdbksz;		/* codebook size in vectors			*/
    double *cdbkup;	/* update information for centroids		*/
    
{

    double *rc;		/* reflection coefficients			*/
    double alpha;	/* prediction error				*/
    double *a;		/* filter coefficients				*/
    int c;		/* iteration counter				*/
    
    if (((a = (double *)calloc(cdbkupdim, sizeof(double))) == NULL) ||
    	((rc = (double *)calloc(cdbkupdim - 1, sizeof(double))) == NULL))
      error("insufficient memory (writecdbk 1)");
    
    if (fwrite(cdbk, sizeof(double), cdbksz*cdbkdim, cdbkfile) !=
      cdbksz*cdbkdim) error("failed to write codebook (writecdbk 2)");
    
    for (c = 0; c < cdbksz; c++) {
        automethx(cdbkup + c*cdbkupdim, cdbkupdim - 1, cdbk + c*cdbkdim + 1,
          &alpha, a, rc);
    	if (fwrite(rc, sizeof(double), cdbkupdim - 1, cdbkfile) != 
    	  cdbkupdim - 1) error("failed to write codebook (writecdbk 3)");
    }
    
    free(a);
    free(rc);
}

